<?php
namespace Admin\Controller;
use Think\Controller;

class AreaController extends CommonController {
	private $M = '';
	public function _initialize(){

	}

	public function ajaxCityList(){
		$pid = $_REQUEST['pid'];
		if( !empty($pid) ){
			$list = $this->getCityList($pid);
			$this->ajaxReturn($list,'成功获取城市列表！',1);
		}else{
			$this->ajaxReturn('','0',0);
		}
	}

	public function ajaxAreaList(){
		$cid = $_REQUEST['cid'];
		if( !empty($cid) ){
			$list = $this->getAreaList($cid);
			$this->ajaxReturn($list,'成功获取区域列表！',1);
		}else{
			$this->ajaxReturn('','0',0);
		}
	}

	public function getProvinceList(){
		$list = $this->M->where( 'pid=0 and cid=0' )->select();
		return $list;
	}

	public function getCityList($pid=0,$field='',$order=''){
		return $list = $this->M->where( 'pid='.$pid.' and cid=0' )->order($order)->field($field)->select();
	}

	public function getAreaList($cid=0, $field='', $order=''){
		return $list = $this->M->where( 'cid='.$cid.' and pid<>0' )->order($order)->field($field)->select();
	}

	public function ajax_city_list(){
		$pid = I('get.pid');
		if( !empty($pid) ){
			$list = city_list($pid);
			$this->ajaxReturn( array('status'=>1, 'info'=>'成功获取城市列表！', 'list'=>$list) );
		}
	}

	public function ajax_area_List(){
		$cid = I('get.cid');
		if( !empty($cid) ){
			$list = area_list($cid);
			$this->ajaxReturn( array('status'=>1, 'info'=>'成功获取区域列表！', 'list'=>$list) );
		}
	}

	public function ajax_city_option(){
		$pid = I('get.pid');
		$option = "<option value='0'>请选择城市</option>";
		if( !empty($pid) ){
			$list = city_list($pid);
			foreach ($list as $key => $val) {
				$option .= "<option value='".$val['id']."'>".$val['name']."</option>";
			}
		}
		$this->ajaxReturn( array('status'=>1, 'info'=>'成功获取城市列表！', 'option'=>$option) );
	}

	public function ajax_area_option(){
		$cid = I('get.cid');
		$option = "<option value='0'>请选择区域</option>";

		if( !empty($cid) ){
			$list = area_list($cid);
			foreach ($list as $key => $val) {
				$option .= "<option value='".$val['id']."'>".$val['name']."</option>";
			}
		}
		$this->ajaxReturn( array('status'=>1, 'info'=>'成功获取区域列表！', 'option'=>$option) );
	}

}